package com.chenailin.www.serviceimpl;

import com.chenailin.www.service.EmailService;
import com.chenailin.www.util.EmailUtil;

import java.util.Random;

/**
 * @author evi
 */
// 实现类
public class EmailServiceImpl implements EmailService {
    @Override
    public void sendEmail(String to, String subject, String content) {

        System.out.println("发送邮件给: " + to);
        System.out.println("主题: " + subject);
        System.out.println("内容: " + content);

        // 实际实现示例（使用JavaMail）：
        /*
        try {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.example.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");

            Session session = Session.getInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username", "password");
                }
            });

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("noreply@example.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(subject);
            message.setText(content);

            Transport.send(message);
        } catch (MessagingException e) {
            throw new RuntimeException("邮件发送失败", e);
        }
        */
    }

    @Override
    public void sendVerificationCode(String email) throws Exception {
        // 生成6位随机码
        String code = generateCode();
        String content = "您的验证码是：" + code + "，5分钟内有效";

        // 实际项目应存储code到Redis（设置5分钟过期）
        EmailUtil.sendEmail(email, "验证码通知", content);
    }

    private String generateCode() {
        return String.format("%06d", new Random().nextInt(999999));
    }
}
